import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//==========业务人员=========
const BusinessWorkbench = {
  path: '/BusinessWorkbench',
  name: 'BusinessWorkbench',
  component: () => import( '../views/page/maintenance/BusinessWorkbench'),
  meta: {title: "工作台", menu: true, icon: 'el-icon-s-marketing'}
}
const AddCardNo = {
  path: '/AddCardNo',
  name: 'AddCardNo',
  component: () => import( '../views/page/maintenance/AddCardNo'),
  meta: {title: "新建号卡", menu: false}
}
const RoleManager = {
  path: '/RoleManager',
  name: 'RoleManager',
  component: () => import( '../views/page/maintenance/RoleManager'),
  meta: {title: "角色管理", menu: true, icon: 'el-icon-s-custom'},
  children: [
    {
      path: 'BusinessUserManager',
      name: 'BusinessUserManager',
      component: () => import( '../views/page/maintenance/BusinessUserManager'),
      meta: {title: "业务人员管理", menu: true, icon: 'el-icon-user-solid'}
    },
    {
      path: 'ShopUserManager',
      name: 'ShopUserManager',
      component: () => import( '../views/page/maintenance/ShopUserManager'),
      meta: {title: "商户管理", menu: true, icon: 'el-icon-user-solid'}
    }
  ]
}
const OperationManager = {
  path: '/OperationManager',
  name: 'OperationManager',
  component: () => import( '../views/page/maintenance/OperationManager'),
  meta: {title: "运维管理", menu: true, icon: 'el-icon-s-grid'},
  children: [
    {
      path: 'MaintenanceManager',
      name: 'MaintenanceManager',
      component: () => import( '../views/page/maintenance/MaintenanceManager'),
      meta: {title: "套餐管理", menu: true, icon: 'el-icon-menu'}
    },
    {
      path: 'AAAANoTypeManager',
      name: 'AAAANoTypeManager',
      component: () => import( '../views/page/maintenance/AAAANoTypeManager'),
      meta: {title: "靓号管理", menu: true, icon: 'el-icon-s-opportunity'}
    },
    {
      path: 'NoCardManager',
      name: 'NoCardManager',
      component: () => import( '../views/page/maintenance/NoCardManager'),
      meta: {title: "号卡管理", menu: true, icon: 'el-icon-phone'}
    }
  ]
}
/*const MaintenanceManager = {
  path: '/MaintenanceManager',
  name: 'MaintenanceManager',
  component: () => import( '../views/page/maintenance/MaintenanceManager'),
  meta: {title: "套餐管理", menu: true}
}
const AAAANoTypeManager = {
  path: '/AAAANoTypeManager',
  name: 'AAAANoTypeManager',
  component: () => import( '../views/page/maintenance/AAAANoTypeManager'),
  meta: {title: "靓号管理", menu: true}
}
const NoCardManager = {
  path: '/NoCardManager',
  name: 'NoCardManager',
  component: () => import( '../views/page/maintenance/NoCardManager'),
  meta: {title: "卡号管理", menu: true}
}*/
/*const BusinessUserManager = {
  path: '/BusinessUserManager',
  name: 'BusinessUserManager',
  component: () => import( '../views/page/maintenance/BusinessUserManager'),
  meta: {title: "业务人员管理", menu: true}
}
const ShopUserManager = {
  path: '/ShopUserManager',
  name: 'ShopUserManager',
  component: () => import( '../views/page/maintenance/ShopUserManager'),
  meta: {title: "商户管理", menu: true}
}*/
const MaintenanceOrderManager = {
  path: '/MaintenanceOrderManager',
  name: 'MaintenanceOrderManager',
  component: () => import( '../views/page/maintenance/MaintenanceOrderManager'),
  meta: {title: "业务工单", menu: true, icon: 'el-icon-s-order'}
}
const HandleOrder = {
  path: '/HandleOrder',
  name: 'HandleOrder',
  component: () => import( '../views/page/maintenance/HandleOrder'),
  meta: {title: "工单办理", menu: false}
}
const NoticeManager = {
  path: '/NoticeManager',
  name: 'NoticeManager',
  component: () => import( '../views/page/maintenance/NoticeManager'),
  meta: {title: "发布公告", menu: true, icon: 'el-icon-info'}
}

//==========商户=========
const ShopIndex = {
  path: '/ShopIndex',
  name: 'ShopIndex',
  component: () => import( '../views/page/merchant/ShopIndex'),
  meta: {title: "商户首页", menu: true, icon: 'el-icon-s-marketing'}
}
const CardOpenBusiness = {
  path: '/CardOpenBusiness',
  name: 'CardOpenBusiness',
  component: () => import( '../views/page/merchant/CardOpenBusiness'),
  meta: {title: "新卡选号", menu: true, icon: 'el-icon-s-goods'}
}
const CardOpen = {
  path: '/CardOpen',
  name: 'CardOpen',
  component: () => import( '../views/page/merchant/CardOpen'),
  meta: {title: "办理开卡", menu: false},
  props: route => ({query: route.query.id})
}
const ShopOrderManager = {
  path: '/ShopOrderManager',
  name: 'ShopOrderManager',
  component: () => import( '../views/page/merchant/ShopOrderManager'),
  meta: {title: "商户工单", menu: true, icon: 'el-icon-s-order'}
}
const ShopOrderView = {
  path: '/ShopOrderView',
  name: 'ShopOrderView',
  component: () => import( '../views/page/merchant/ShopOrderView'),
  meta: {title: "工单预览", menu: false}
}
const NoticeView = {
  path: '/NoticeView',
  name: 'NoticeView',
  component: () => import( '../views/page/merchant/NoticeView'),
  meta: {title: "商户公告", menu: true, icon: 'el-icon-info'}
}

const routes = [
  {
    path: '/Login',
    name: 'Login',
    component: () => import( '../views/page/Login'),
    meta: {title: "登录", menu: false}
  }, {
    path: '/ResetPass',
    name: 'ResetPass',
    component: () => import( '../views/page/ResetPass'),
    meta: {title: "修改密码", menu: false}
  }
]

const businessRoutes = [BusinessWorkbench, MaintenanceOrderManager, HandleOrder, NoticeManager, ShopOrderView];

const shopRoutes = [ShopIndex, CardOpenBusiness, CardOpen, ShopOrderManager, ShopOrderView, NoticeView, {
  path: '/MaintenanceManager',
  name: 'MaintenanceManager',
  component: () => import( '../views/page/maintenance/MaintenanceManager'),
  meta: {title: "套餐详情", menu: false}
},];

const superAdminRoutes = [...businessRoutes,
  AddCardNo, RoleManager, OperationManager, ...shopRoutes, {
    path: '/OperationLog',
    name: 'OperationLog',
    component: () => import( '../views/page/OperationLog'),
    meta: {title: "操作日志", menu: true, icon: 'el-icon-s-tools'}
  }];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.setMenu = () => {
  const userInfo = JSON.parse(localStorage.getItem('userInfo'));
  //动态路由加载角色菜单
  if ('1' === userInfo?.isSuperAdmin) {
    router.addRoutes(superAdminRoutes);
    // router.options.routes.push(...superAdminRoutes);
    router.options.routes = [...routes, ...superAdminRoutes];
  } else if ('01' === userInfo?.userType) {
    router.addRoutes(businessRoutes);
    // router.options.routes.push(...businessRoutes);
    router.options.routes = [...routes, ...businessRoutes];
  } else if ('02' === userInfo?.userType) {
    router.addRoutes(shopRoutes);
    // router.options.routes.push(...shopRoutes);
    router.options.routes = [...routes, ...shopRoutes];
  }
};

//路由导航守卫
router.beforeEach((to, from, next) => {
  if (router.options.routes.length === routes.length && to.path !== "/Login") {
    router.setMenu();
  }
  const token = localStorage.getItem('token');
  if (to.matched.length === 0 && to.path !== '/') next(to);//关键代码,动态路由关键,防止页面空白
  if (to.name !== 'Login' && !token) next({name: 'Login'})
  else next()
});

export default router
